
package RMI;

import Dao.DaoHospede;
import Dao.DaoHotel;
import Fachada.Fachada;
import Modelo.Hospede;
import Modelo.Hotel;
import Modelo.Quarto;
import Modelo.Reserva;
import Util.Utils;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

/**
 *
 * @author kassio
 * @author pablo
 * 
 */
public class ServicosImplementacao extends UnicastRemoteObject implements ServicosIF {

    private static ServicosIF servicos;
    private Fachada fachada = Fachada.getInstance();

    public synchronized static ServicosImplementacao getInstance() throws RemoteException {
        if (servicos == null) {
            servicos = new ServicosImplementacao();
        }
        return (ServicosImplementacao) servicos;
    }

    private ServicosImplementacao() throws RemoteException {
    }

    @Override
    public boolean addHospede(Hospede h) throws RemoteException {
        DaoHospede dao = new DaoHospede();
        return dao.add(h);
    }

    @Override
    public boolean removeHospede(Hospede h) throws RemoteException {
        DaoHospede dao = new DaoHospede();
        return dao.remove(h);
    }

    @Override
    public boolean editHospede(Hospede h) throws RemoteException {
        DaoHospede dao = new DaoHospede();
        dao.edit(h);
        return true;
    }

    @Override
    public Hospede findHospede(int id) throws RemoteException {
        DaoHospede dao = new DaoHospede();
        return dao.find(id);
    }

    @Override
    public List<Hospede> getHospedes() throws RemoteException {
        DaoHospede dao = new DaoHospede();
        return dao.findAll();
    }

    //hotel
    @Override
    public boolean addHotel(Hotel h) throws RemoteException {
        DaoHotel dao = new DaoHotel();
        return dao.add(h);
    }

    @Override
    public Hotel findHotel(int id) throws RemoteException {
        DaoHotel dao = new DaoHotel();
        return dao.find(id);
    }

    @Override
    public List<Hotel> getHoteis() throws RemoteException {
        return this.fachada.getDaoHotel().findAll();
    }

    @Override
    public boolean removeHotel(Hotel h) throws RemoteException {
        DaoHotel dao = new DaoHotel();
        return dao.remove(h);
    }

    @Override
    public boolean editHotel(Hotel h) throws RemoteException {
        new DaoHotel().edit(h);
        return true;
    }

    //reserva
    @Override
    public int addReserva(Reserva r) throws RemoteException {
        
        int result = fachada.getDaoReserva().verificaSePodeReservarQuarto(r); 
        try {
            if (result == Utils.SUCESSO) {
                fachada.getDaoReserva().add(r);
                return result;
            } else {
                return result;
            }
        } catch (Exception e) {
            return result;
        }
    }

    @Override
    public int removeReserva(Reserva r) throws RemoteException {

        this.fachada.getDaoReserva().remove(r);
        return Utils.SUCESSO;
    }

    @Override
    public int editReserva(Reserva r) throws RemoteException {

        int result = fachada.getDaoReserva().verificaSePodeReservarQuarto(r);
        try {
            if (result == Utils.SUCESSO) {
                this.fachada.getDaoReserva().edit(r);
                return result;
            } else {
                return result; 
            }
        } catch (Exception e) {
            return result;
        }
    }

    @Override
    public Reserva findReserva(int id) throws RemoteException {
        return this.fachada.getDaoReserva().find(id);
    }

    @Override
    public List<Reserva> getReservas() throws RemoteException {
        return this.fachada.getDaoReserva().findAll();
    }

    @Override
    public void helloworld() throws RemoteException {
        System.out.print("Hello World!");
    }

    @Override
    public int addQuarto(Quarto q) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int removeQuarto(Quarto q) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int editQuarto(Quarto q) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Quarto findQuarto(int id) throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Quarto> findAll() throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
